package com.jiang.redis.single;

import redis.clients.jedis.Jedis;

/**
 * @author jiangwz
 * @create 2019/3/7.
 */
public class SubThread extends Thread {


    private final Jedis jedis;
    private final Subscriber01 subscriber = new Subscriber01();

    private final String channel = "mychannel";

    public SubThread(Jedis jedis) {
        super("SubThread");
        this.jedis = jedis;
    }

    @Override
    public void run() {
        System.out.println(String.format("subscribe redis, channel %s, thread will be blocked", channel));
        try {
            jedis.subscribe(subscriber, channel);    //通过subscribe 的api去订阅，入参是订阅者和频道名
        } catch (Exception e) {
            System.out.println(String.format("subsrcibe channel error, %s", e));
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }
}
